package com.starbugs.wehcat.aop;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;

public class JDKProxy  implements InvocationHandler {
  private Calc calc ;

  public JDKProxy(Calc calc) {
    this.calc = calc;
  }

  /*
   * 生产ArithmeticCalculator代理的方法
   */
  public  Calc createProxy(){
    Calc calc1 = (Calc) Proxy.newProxyInstance(
        calc.getClass().getClassLoader(), // 类加载器
        calc.getClass().getInterfaces(),
        this
    );
    return calc1;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    // 判断方法名是不是add
    if("add".equals(method.getName())){
      System.out.println(new Date());
    }

    return method.invoke(calc, args);
  }
}
